<template>
  <div class="app-container">
    <el-card class="box-card search-div" v-show="showSearch">
        <el-form :inline="true" :model="listQuery" ref="queryForm">
          <el-form-item label="租户编号">
            <el-input v-model="listQuery.tenantCode" :maxlength="12"  clearable></el-input>
          </el-form-item>
          <el-form-item label="租户名称">
            <el-input v-model="listQuery.tenantName" :maxlength="50"  clearable></el-input>
          </el-form-item>
         <el-button type="primary" @click="handleSearch" icon="el-icon-search">搜索</el-button>
         <el-button @click="handleResetQuery" icon="el-icon-refresh">重置</el-button>
     </el-form>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" v-hasPermi="['sys_sysTenant_add']" class="el-icon-plus" @click="add">新增</el-button>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </div>
      <el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row class="table-list">
        <el-table-column label="序号" type="index" min-width="5%"/>
        <el-table-column align="center" label="租户编号" prop="tenantCode"/>
        <el-table-column align="center" label="租户名称" prop="tenantName"/>
        <el-table-column align="center" label="联系人" prop="linkman"/>
        <el-table-column align="center" label="联系电话" prop="contactNumber"/>
        <el-table-column align="center" label="账号额度" prop="accountnumber">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.accountnumber==-1">无限制</el-tag>
            <span v-else>{{scope.row.accountnumber}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="有效天数" prop="expireDays" :formatter="expireDaysFormat" >
        </el-table-column>
        <el-table-column align="center" label="操作" width="160">
          <template slot-scope="scope">
            <el-button plain size="mini" v-hasPermi="['sys_sysTenant_edit']" type="violet" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button plain size="mini" v-hasPermi="['sys_sysTenant_remove']" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
     <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </el-card>

    <!--弹出框-->
    <el-dialog :title="dialogTitle" :width="'600px'" :visible.sync="showDialog" :close-on-click-modal="false">
      <el-form :rules="rules" ref="savesysTenantData" :model="savesysTenantData" label-width="100px">
        <el-form-item label="租户名称:" prop="tenantName">
            <el-input v-model="savesysTenantData.tenantName" :maxlength="50"  placeholder="最多输入50个字符" clearable show-word-limit></el-input>
            </el-form-item>
        <el-form-item label="域名地址:" prop="domain">
            <el-input type="textarea" rows="3" v-model="savesysTenantData.domain" :maxlength="255"  placeholder="最多输入255个字符" clearable show-word-limit></el-input>
            </el-form-item>
        <el-form-item label="系统背景:" prop="backgroundUrl">
            <el-input type="textarea" rows="3" v-model="savesysTenantData.backgroundUrl" :maxlength="500"  placeholder="最多输入500个字符" clearable show-word-limit></el-input>
            </el-form-item>
        <el-form-item label="联系人:" prop="linkman">
            <el-input v-model="savesysTenantData.linkman" :maxlength="20"  placeholder="最多输入20个字符" clearable show-word-limit></el-input>
            </el-form-item>
        <el-form-item label="联系电话:" prop="contactNumber">
            <el-input v-model="savesysTenantData.contactNumber" :maxlength="20"  placeholder="最多输入20个字符" clearable show-word-limit></el-input>
            </el-form-item>
        <el-form-item label="账号额度:" prop="accountnumber">
            <el-input-number v-model="savesysTenantData.accountnumber" :min="0" :max="999999" style="width: 100%;"></el-input-number>
            </el-form-item>
        <el-form-item label="有效天数:" prop="expireDays">
            <el-select v-model="savesysTenantData.expireDays" placeholder="请选择有效天数" clearable style="width: 100%;"><el-option v-for="dict in expireDaysOptions" :key="dict.dictKey" :label="dict.dictValue" :value="parseInt(dict.dictKey)" /></el-select>
            </el-form-item>
        <el-form-item label="数据源ID:" prop="datasourceId">
            <el-input-number v-model="savesysTenantData.datasourceId" :min="0" :max="999999" style="width: 100%;"></el-input-number>
            </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button type="primary" @click="onSave('savesysTenantData')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as sysTenant from './js/tenant'
import { mapGetters } from 'vuex'

export default {
  name: 'tenantList',
  data() {
    return {
      list: null,
      // 遮罩层
      listLoading: true,
      // 总条数
      total: 0,
      // 显示搜索条件
      showSearch: true,
      listQuery: {
        page: 1, limit: 10,   tenantCode: '',  tenantName: '',                     },
                                                 expireDaysOptions:[],
  showDialog: false,
      savesysTenantData: {  id: '',  tenantCode: '',  tenantName: '',  domain: '',  backgroundUrl: '',  linkman: '',  contactNumber: '',  accountnumber: '',  expireDays: '',  datasourceId: '', },
      rules: {
        tenantName: [{ required: true, message: '请输入租户名称', trigger: 'blur' }],
        expireDays: [{ required: true, message: '请输入有效天数', trigger: 'blur' }],
      },
      dialogTitle: '添加租户管理'
    }
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  created() {
  	this.init()
    this.getList()
  },
  methods: {
    // 字典列表初始
    init(){
      this.getDictByCode("tenant_expire_days").then(response => {
        this.expireDaysOptions = response.data;
      });                
    },
    // 字典值列表显示
    expireDaysFormat(row,column){
      return this.selectDictLabel(this.expireDaysOptions, row.expireDays);
    },                        
    getList() { // 获取所有租户管理
      this.listLoading = true
      sysTenant.list(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleResetQuery(){
      this.resetForm("queryForm")
      this.handleSearch()
    },
    //删除
    handleDelete(id) {
      this.$confirm('请确定是否删除该数据', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        sysTenant.removeByIds(id).then((response) => {
          this.$message({ type: 'success', message: '操作成功!' })
          this.getList()
        })
      }).catch(function(error) { console.log(error) })
    },
    // 新增
    add(pit, index) {
      this.showDialog = true
      this.dialogTitle = '添加租户管理'
      // 清空表单数据
      this.$nextTick(() => {
        this.resetForm('savesysTenantData')
      })
    },
    // 编辑
    handleEdit(index, row) {
      this.showDialog = true
      this.dialogTitle = '编辑租户管理'
      this.$nextTick(() => {
        this.resetForm('savesysTenantData')
         // 需要拷贝一下，不然会清空列表
        let tmpRow = JSON.parse(JSON.stringify(row));
        this.savesysTenantData = tmpRow
      })
    },
    // 保存
    onSave(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          sysTenant.editSave(this.savesysTenantData).then((response) => {
            this.$message({ type: 'success', message: '操作成功!' })
            this.getList()
          	this.showDialog = false
          }).catch(function(error) {
            console.log(error)
          })
        }
      })
    }
  }
}
</script>
